package edu.umassd.visual;

import java.io.BufferedReader;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.springframework.stereotype.Component;

import edu.umassd.raddacl.util.FilesUtil;

@Component
public class AttributeNames implements Iterable<String>
{
	private List<String> names = new ArrayList<>();

	private StringBuilder temp = new StringBuilder();

	/**
	 * Looks for a file with the extension ".titles", in the same directory as
	 * the specified path.
	 * 
	 * @param location
	 *            - the path to the output file.
	 * @throws IOException
	 *             - if the extensions path could not loaded.
	 */
	public void loadNames(Path location) throws IOException
	{
		temp.append("Attribute 0");

		Path p = FilesUtil.changeExtension(location, "titles");

		BufferedReader reader = Files.newBufferedReader(p,
				Charset.defaultCharset());

		String name = null;

		while ((name = reader.readLine()) != null)
		{
			names.add(name);
		}
	}

	public String getName(int index)
	{
		if (names.size() == 0)
		{
			temp.setCharAt(temp.length() - 1, Character.forDigit(index, 9));

			return temp.toString();
		}

		return names.get(index);
	}

	@Override
	public Iterator<String> iterator()
	{
		return names.iterator();
	}
}
